package com.sample.kotlin.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.Gravity
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.graphics.toColorInt
import com.sample.base.utils.dp
import java.util.*

/**
 *  @Author Created by Z on 2020/4/14 星期二 16:55
 *  @Desc :
 */
class CoderView @JvmOverloads constructor(
  context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {

  private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
    Log.e("CoderView", "Paint")
    style = Paint.Style.STROKE
    color = "#03DAC5".toColorInt()
    strokeWidth = 6.dp.toFloat()
  }
  private val codeList = arrayOf(
    "kotlin",
    "android",
    "java",
    "http",
    "https",
    "okhttp",
    "retrofit",
    "tcp/ip"
  )

  init {
    Log.e("CoderView", "init")
    setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f)
    setTextColor(Color.WHITE)
    gravity = Gravity.CENTER
    setBackgroundColor("#6200EE".toColorInt())
    updateCode()
  }


  fun updateCode() {
    val nextInt = Random().nextInt(codeList.size)
    text = codeList[nextInt]
  }

  override fun onDraw(canvas: Canvas) {
    canvas.drawLine(0f, height.toFloat(), width.toFloat(), 0f, paint)
    super.onDraw(canvas)
  }
}